/**********************************************************************/
/*
	Author: Michelle Han, Adapted from Nikhil Kumar
	Last Updated: July 2024
   	Description: Cleans raw March 2018 SUSENAS data. Outputs deidentified
	cleaned SUSENAS data merged with PMO data.

	This cleaning file should output 2 different datasets:
	1. Cleaned Susenas data:
	sus_mar18_deid_clean
	2. Subset of Susenas person-batch data matched with PMO:
	sus_mar18_deid_clean_merged
*/
/**********************************************************************/

*******************************************
* Setup
* include filepaths 
  	if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"
	cap log close
	global prefix: display %tdCYND td(`c(current_date)')
	log using "$KP_logs/${prefix}_clean_SUSENAS_mar2018.txt", text replace

* Load the data

* Save consumption data in tempfile
	import dbase "$KP_deid_susenas/Raw/Household/dbf/mar18/blok43.dbf", clear

	keep URUT FOOD NONFOOD

	rename URUT urut
	rename FOOD food
	rename NONFOOD nonfood

	tempfile cons
	sa `cons'

	use "$KP_deid_susenas/Raw/Individual/SUSENAS_MAR18_deid.dta", clear // individual
	merge m:1 urut using "$KP_deid_susenas/Raw/Household/susenas18mar_rt_diseminasi.dta", keep(1 3) nogen // household
	merge m:1 urut using `cons', assert(3) nogen // consumption

*******************************************
* Recode yes/no questions 

	la def yesno 0 "No" 1 "Yes"

	local yesno_vars r1801* ///

	foreach var of varlist `yesno_vars' {
		assert `var' == 1 | `var' == 5 | `var' == .
		recode `var' (1=1) (5=0)
		replace `var' = 0 if mi(`var') // for questions dependent on previous skip logic
		la val `var' yesno
	}


*******************************************
* Demographics

* City (kotamadya) dummy
	gen kode_kab = r102
	gen city = kode_kab >= 70

* Gender
	gen gender = r405 == 1
	la def gender 1 "Male" 0 "Female"
	la val gender gender

	gen female = r405 == 2
	la def female 1 "Female" 0 "Male"
	la val female female

* Marital status
	gen marital_status = r404

* Bucket age
	gen age =  r407
	recode age (0/20 = 1) (21/40 = 2) (41/60 = 3) (61/80 = 4) (81/100 = 5), gen(age_cat)

* Dummy for age <=30 
	gen young = 0 
	replace young = 1 if age <= 30
	la def young 0 "Over 30 Years Old" 1 "30 and Under"
	la val young young 
	
* Education (translated to years of schooling)
* https://www.seameo.org/Main_about/95
	gen educ = r615
	gen school_years = .
	replace school_years = 6 if inlist(educ, 1, 2, 3, 4) // Paket A, SDLB, SD, MI
	replace school_years = 9 if inlist(educ, 5, 6, 7, 8) // Paket B, SMP LB, SMP, MTs
	replace school_years = 12 if inlist(educ, 9, 10, 11, 12, 13, 14) // Paket C, SMLB, SMA, MA, SMK, MAK
	replace school_years = 13.5 if educ == 15 // Diploma I/II
	replace school_years = 15 if educ == 16 // Diploma III
	replace school_years = 16 if inlist(educ, 17, 18) // Diploma IV / SI
	replace school_years = 18 if inlist(educ, 19) // SII/Profesi
	replace school_years = 20 if educ == 20 // SIII

* Dummy for educated (graduating high school)
	gen educated = school_years > 12 
	la def educated 0 "Less than 12 Years Schooling" 1 "12 or More Years of Schooling"
	la val educated educated

* Number of HH members
	gen hh_size = r301

* Consumption
* Log per-capita household consumption
	gen log_cons = log(exp_cap)

	gen cons_adj = exp_cap
	sum cons_adj if cons_adj > 0, d
	replace cons_adj = r(p99) if cons_adj > r(p99) & !mi(cons_adj)
	replace cons_adj = r(p1) if cons_adj < r(p1) & !mi(cons_adj)
	sum cons_adj, d
	gen log_adj_cons = log(cons_adj)

	* food per cap
	gen cons_food = food / r301
	gen lcon_food = log(cons_food)

	gen cons_adj_food = cons_food
	sum cons_adj_food, d
	replace cons_adj_food = r(p99) if cons_adj_food > r(p99) & !mi(cons_adj_food)
	replace cons_adj_food = r(p1) if cons_adj_food < r(p1) & !mi(cons_adj_food)
	replace cons_adj_food = cons_adj_food / 1000
	gen lcon_adj_food = log(cons_adj_food)

	* non-food per cap
	gen cons_nfood = nonfood / r301
	gen lcon_nfood = log(cons_nfood)

	gen cons_adj_nfood = cons_nfood
	sum cons_adj_nfood, d
	replace cons_adj_nfood = r(p99) if cons_adj_nfood > r(p99) & !mi(cons_adj_nfood)
	replace cons_adj_nfood = r(p1) if cons_adj_nfood < r(p1) & !mi(cons_adj_nfood)
	replace cons_adj_nfood = cons_adj_nfood / 1000
	gen lcon_adj_nfood = log(cons_adj_nfood)

* Do you (applicant) live in same kabupaten you were born in?
	gen born_kab = r603
	gen born_live_same = born_kab == kode_kab

* Durable goods 
* 5.5 kilos or more LPG canister
	gen dur_canister = r1801a 

* fridge/freezer
	gen dur_fridge = r1801b 

*  air conditioner	
	gen dur_aircon = r1801c 

* water heater 	
	gen dur_waterheat = r1801d

* telephone 
	gen dur_phone = r1801e 

* computer
	gen dur_computer = r1801f 

* gold	
	gen dur_gold = r1801g 

* motorcycle	
	gen dur_bike = r1801h 

* boat	
	gen dur_boat = r1801i

* motorboat	
	gen dur_motorboat = r1801j

* car	 
	gen dur_car = r1801k 

* flat television	
	gen dur_tv = r1801l 

* land	
	gen dur_land = r1801m

* Check
	foreach var of varlist dur_* {
		tab `var', m
		la val `var' yesno
	}

*******************************************
* Telecom

* Internet use
	gen use_internet = r716 == 1

* In the last 3 months, have you (applicant) accessed internet using PC/laptop/tablet
	gen use_computer = r715 == 1
	gen use_internet_comp = use_internet * use_computer

* In the last 3 months, have you (applicant) accessed internet using cellphone
	gen use_phone = r713 == 1
	gen use_internet_phone = use_internet * use_phone

* Own phone
	gen mobile_ownership = r714==1


*******************************************
* Employment

* Employed in week prior to survey (801)
	gen employed = inlist(r805, 1, 2, 3, 4, 5)

* Employment status
	gen employment_status = r805
	gen self_employed = r805 == 1
	gen business_owner = r805 == 2 | r805 == 3
	gen self_emp_bus_owner = (r805 == 1 | r805 == 2 | r805 == 3)
	gen perm_worker = r805 == 4
	gen freelancer = r805 == 5
	gen family_unpaid = r805 == 6

	label define emp 1 "Self-employed" 2 "Business with Temporary Employees" ///
			3 "Business with Permanent Employees" ///
			4 "Permanent Worker" 5 "Freelancer" 6 "Family/Unpaid Worker", replace
	lab val employment_status emp

	tab employed employment_status, m

* 806, 807- Hours Worked
	gen hrs_worked_main = r806
	replace hrs_worked_main = 0 if employed == 0
	gen hrs_worked_all = r807
	replace hrs_worked_all = 0 if employed == 0	

* Industry
	gen ind_code = r804

* In the last week, did you work or have work or a business but temporarily not working?  (same as SAKARNAS 9A + 10A --can also stack and add a dummy)
	gen temp_not_work = r803
	recode temp_not_work (1=1) (5=0)
	replace temp_not_work = 0 if employed
	tab temp_not_work, m
	tab temp_not_work employment_status, m
	tab temp_not_work employed, m

* Household business owners
	bys urut: egen hh_business = max(business_owner)
	bys urut: egen hh_emp_self = max(self_employed)

* Does anyone in the household own a business with employees (either formal or informal) -- 706 == 2 or 3
	gen with_emp = inlist(r805, 2, 3)
	bys urut: egen with_emp_HH_S = max(with_emp)


*******************************************
* Assets

* Motorbike
	gen motorbike = r1801h == 1

* Gold/jewelry
	gen gold_jewel = r1801g == 1

* Flat screen TV
	gen flat_TV = r1801l == 1

*House characteristics:
*# sqm of the house
	gen hh_area = r1504

*******************************************
* Program Receipt

* 1604: receive BPNT 
	gen ever_BPNT = r1604 ==1
	tab ever_BPNT

* 1608: Family Card Hejahtera (KKS)
	gen receive_KKS = (r1608 == 1 | r1608 == 2)
	la val receive_KKS yesno
	tab receive_KKS

* 1609:  PKH last year
	gen receive_pkh_year = r1609  ==1
	tab receive_pkh_year

* 1610b:  PKH Now
	gen receive_pkh_now = r1610b ==1
	tab receive_pkh_now

* 1611: any assistance from local governments
	gen local_assist = r1611 ==1
	tab local_assist

* 1612a: routine assistance from local governments
	gen r_assist = r1612a ==1
	tab r_assist 

* 1612b: non-routine assistance from local governments
	gen nr_assist = r1612b ==1
	tab nr_assist

******************************************************
* check duplicates
  duplicates tag anon_id4, gen(anon_id4_dupe)
  tab anon_id4_dupe

* No duplicates
  count if anon_id4_dupe > 0 & !mi(anon_id4)
  assert r(N) == 0
  drop anon_id4_dupe

/*----------------------------------------------------*/
              * Export cleaned data
/*----------------------------------------------------*/

  rename fwt weight
  datasignature 
  if "`r(datasignature)'" == "1131825:430(47406):3022610694:905352659" {
    save "$KP_deid_susenas/Clean/sus_mar18_deid_clean.dta", replace
      }
  else {
    di as err "Careful, your machine produces a different dataset"
    stop
		}
  

/*----------------------------------------------------*/
          * Merge with admin data and export
/*----------------------------------------------------*/

	gen sus_round = 1
	keep if !mi(anon_id4)
	drop r1* r2* r3* r4* r5* r6* r7* r8* r9*

	* Format date incentive 
	rename date_incentive rawtime 
	gen date_incentive = date(rawtime, "YMD")
	format date_incentive %td
	summ date_incentive 
	drop rawtime
	
	* Merge with PMO data
	merge 1:m anon_id4 using "$KP_deid_admin/Clean/pmo_b1-22_clean_long_deid.dta", nogen keep(3)

	compress
	datasignature 
  if "`r(datasignature)'" == "154947:161(55742):2570431324:2784434213" {
    save "$KP_deid_susenas/Clean/sus_mar18_deid_clean_merged.dta", replace
      }
  else {
    di as err "Careful, your machine produces a different dataset"
    stop
		}
	
